package com.zg.autoflow.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zg.autoflow.req.AutoflowExtStepConfigFindReq;
import com.zg.autoflow.req.AutoflowExtStepConfigReq;
import com.zg.autoflow.req.AutoflowMainCodeReq;
import com.zg.autoflow.req.StepConfigReq;
import com.zg.autoflow.service.AutoflowExtStepConfigService;
import com.zg.autoflow.vo.AutoflowExtStepConfigFindVo;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoflow.dao.AutoflowExtStepConfigDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowExtSpecialFinal;
import com.zg.common.core.dao.autoflow.entity.AutoflowExtStepConfig;
import com.zg.common.core.dao.autoflow.entity.AutoflowMain;
import com.zg.common.core.dao.autoform.DiyForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
@Api(tags = {"表单权限"})
public class AutoflowExtStepConfigController {
	private final AutoflowExtStepConfigDao autoflowExtStepConfigDao;
	private final AutoflowExtStepConfigService autoflowExtStepConfigService;

	@PostMapping("/autoflow/ext/step/info")
	@ApiOperation("查询环节配置信息")
	public R<AutoflowExtStepConfigFindVo> findStepInfo(@RequestBody@Valid AutoflowExtStepConfigFindReq req) {
		AutoflowExtStepConfigFindVo obj = autoflowExtStepConfigService.findStepInfo(req);
		return R.success(obj);
	}

	@PostMapping("/autoflow/ext/step/hasConfig")
	@ApiOperation("是否存在配置信息")
	public R<Boolean> hasConfig(@RequestBody@Valid AutoflowExtStepConfigReq req) {
		//查询环节配置
		long count = autoflowExtStepConfigDao.count(new QueryWrapper<AutoflowExtStepConfig>()
				.lambda()
				.eq(AutoflowExtStepConfig::getFlowCode, req.getFlowCode())
				.eq(AutoflowExtStepConfig::getStepNo, req.getStepNo()));
		return R.success(count == 1);
	}

	@PostMapping("/autoflow/ext/step/form")
	@ApiOperation("查询流程单据")
	public R<List<DiyForm>> listFlowForm(@RequestBody@Valid AutoflowMainCodeReq req) {
		List<DiyForm>  list=autoflowExtStepConfigService.listFlowForm(req);
		return R.success(list);
	}

	@PostMapping("/autoflow/ext/step/saveConfig")
	@ApiOperation("保存环节配置")
	public R saveStepConfig(@RequestBody@Valid StepConfigReq stepConfigVo) {
		autoflowExtStepConfigService.saveStepConfig(stepConfigVo);
		return R.success();
	}

	@PostMapping("/autoflow/ext/flow/prev")
	@ApiOperation("查询关联流程")
	public R<List<AutoflowMain>> listPrevFlow(@RequestBody@Valid AutoflowMainCodeReq req) {
		List<AutoflowMain> list =autoflowExtStepConfigService.listPrevFlow(req);
		return R.success(list);
	}

	@PostMapping( "/autoflow/ext/flow/special")
	@ApiOperation("查询个性化单据")
	public R<List<AutoflowExtSpecialFinal>> listSpecialForm(@RequestBody@Valid AutoflowMainCodeReq req) {
		List<AutoflowExtSpecialFinal> list =autoflowExtStepConfigService.listSpecialForm(req);
		return R.success(list);
	}

	@PostMapping( "/autoflow/ext/step/config")
	@ApiOperation("查询当前环节的配置")
	public R<AutoflowExtStepConfigFindVo> findStepForm(@RequestBody@Valid AutoflowExtStepConfigReq req) {
		AutoflowExtStepConfigFindVo vo =autoflowExtStepConfigService.findStepForm(req);
		return R.success(vo);
	}
}
